/**
 * 用于显示数据创建时间的控件，将会把创建时间的long数据转换为date数据
 */
Ext.define('App.commons.grid.column.KpiColumn', {
    extend: 'Ext.grid.column.Number',
    alias: ['widget.KpiColumn', 'widget.kpicolumn'],

    bindFormatter: function (format) {
        var me = this;
        return function (value, metaData, record, rowIndex, colIndex, store, view) {
            if (Ext.isEmpty(value) || value == '%' || isNaN(value)) {
                return '- ';
            }

            value = parseFloat(value);


            var cc = metaData.column;
            if (!Ext.isEmpty(cc.judgmentStateMode)) {
                value = me.judgmentState(cc, format, value, record);
            }
            return value;
        };
    },
    bindRenderer: function (renderer) {
        var me = this;
        //<debug>
        if (renderer in Ext.util.Format) {
            Ext.log.warn('Use "formatter" config instead of "renderer" to use ' +
                'Ext.util.Format to format cell values');
        }
        //</debug>

        me.hasCustomRenderer = true;

        return function () {
            return Ext.callback(renderer, me.rendererScope, arguments, 0, me);
        };
    },
    renderer: function (value, metaData, record) {
        if (Ext.isEmpty(value) || value == '%' || isNaN(value)) {
            return '- ';
        }
        value = parseFloat(value);


        var cc = metaData.column;
        if (!Ext.isEmpty(cc.judgmentStateMode)) {
            value = me.judgmentState(cc, null, value, record);
        }

        return value;
    },
    judgmentState: function (cc, format, value, record) {
        var me = this;

        var formatedValue = value;
        if (!Ext.isEmpty(format)) {
            formatedValue = format(value, me.rendererScope || me.resolveListenerScope());
        }
        if (record.get('迭代') != '基线' && record.get('迭代') != '目标') {
            var html = '<div style="height: 100%;width: 100%;" title="{0}"><img src="css/icons/{1}" style="margin-right:5px;"/>{2}</div>';
            var title = [];
            if (!Ext.isEmpty(cc.baselinevalueue)) {
                title.push("基线值：" + format(cc.baselineValue, me.rendererScope || me.resolveListenerScope()));
            }
            if (!Ext.isEmpty(cc.maxValue)) {
                title.push("目标值：" + format(cc.targetValue, me.rendererScope || me.resolveListenerScope()));
            }
            if (!Ext.isEmpty(cc.minValue)) {
                title.push("最小值：" + format(cc.minValue, me.rendererScope || me.resolveListenerScope()));
            }
            if (!Ext.isEmpty(cc.maxValue)) {
                title.push("最大值：" + format(cc.maxValue, me.rendererScope || me.resolveListenerScope()));
            }
            if (!Ext.isEmpty(cc.targetValue) && !Ext.isEmpty(cc.maxValue) && !Ext.isEmpty(cc.minValue)) {
                if (cc.judgmentStateMode == '上限优先') {
                    if (value >= cc.targetValue) {
                        return Ext.String.format(html, title.join("，"), 'circular-green.png', formatedValue);
                    } else if (value >= cc.minValue && value < cc.targetValue) {
                        return Ext.String.format(html, title.join("，"), 'circular-yellow.png', formatedValue);
                    } else if (value < cc.minValue) {
                        return Ext.String.format(html, title.join("，"), 'circular-red.png', formatedValue);
                    }
                }
                if (cc.judgmentStateMode == '下限优先') {
                    if (value <= cc.targetValue) {
                        return Ext.String.format(html, title.join("，"), 'circular-green.png', formatedValue);
                    } else if (value > cc.targetValue && value <= cc.maxValue) {
                        return Ext.String.format(html, title.join("，"), 'circular-yellow.png', formatedValue);
                    } else if (value > cc.maxValue) {
                        return Ext.String.format(html, title.join("，"), 'circular-red.png', formatedValue);
                    }
                }
                if (cc.judgmentStateMode == '均值优先') {
                    if ((value >= (cc.minValue + (cc.targetValue - cc.minValue)) / 2) && (value <= (cc.targetValue + (cc.maxValue - cc.targetValue)) / 2)) {
                        return Ext.String.format(html, title.join("，"), 'circular-green.png', formatedValue);
                    } else if (value >= cc.minValue &&
                        ((value < (cc.minValue + (cc.targetValue - cc.minValue) / 2) || value > (cc.targetValue + (cc.maxValue - cc.targetValue) / 2))) &&
                        value <= cc.maxValue
                    ) {
                        return Ext.String.format(html, title.join("，"), 'circular-yellow.png', formatedValue);
                    } else if (value > cc.maxValue || value < cc.minValue) {
                        return Ext.String.format(html, title.join("，"), 'circular-red.png', formatedValue);
                    }
                }
            } else {
                var html = '<div style="height: 100%;width: 100%;" title="{0}">{1}</div>';
                return Ext.String.format(html, title.join("，"), formatedValue);
            }
        }
        return formatedValue;
    }
})